package com.zhang.springcloud.feignservice.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhanghaolian
 * @date 2021/6/4 20:31
 */
@RestController
@Slf4j
public class FeignController {

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 测试服务是否已启动
     * @return
     */
    @GetMapping("/testFeignService")
    public String testFeignService() {
        log.info("test web service ok");
        return "service already started up";
    }

    /**
     * 获取服务列表
     */
    @GetMapping("/getServerList")
    public void getServerList() {
        List<String> services = discoveryClient.getServices();
        for (String serviceName: services) {
            List<ServiceInstance> instances = discoveryClient.getInstances(serviceName);
            for (ServiceInstance instance: instances) {
                System.out.println(instance);
            }
        }
    }
}
